#include "mainwindow.h"
#include "messagetype.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDebug>
#include <QFont>
#include <QPalette>
#include "patientdashboard.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), tcpSocket(new QTcpSocket(this)), dashboard(nullptr) {
    setupUI(); // 设置用户界面
}

MainWindow::~MainWindow() {
    if (tcpSocket) {
        tcpSocket->disconnectFromHost();
        tcpSocket->waitForDisconnected();
        delete tcpSocket;
    }
}

void MainWindow::setupUI() {
    setWindowTitle("患者登录系统");
    setFixedSize(600, 700); // 增宽和增高

    // 设置背景颜色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(220, 240, 255)); // 柔和的颜色
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // 登录界面布局
    QVBoxLayout *loginLayout = new QVBoxLayout;
    loginLayout->setContentsMargins(40, 40, 40, 40);
    loginLayout->setSpacing(15);

    QLabel *loginTitleLabel = new QLabel("患者登录", this);
    loginTitleLabel->setFont(QFont("Arial", 24, QFont::Bold));
    loginTitleLabel->setAlignment(Qt::AlignCenter);
    loginLayout->addWidget(loginTitleLabel);

    // 角色选择
    roleComboBox = new QComboBox(this);
    roleComboBox->addItems({"患者", "医生"});
    loginLayout->addWidget(roleComboBox);

    // 用户名
    usernameEdit = new QLineEdit(this);
    usernameEdit->setPlaceholderText("请输入用户名");
    loginLayout->addWidget(usernameEdit);

    // 密码
    passwordEdit = new QLineEdit(this);
    passwordEdit->setPlaceholderText("请输入密码");
    passwordEdit->setEchoMode(QLineEdit::Password);
    loginLayout->addWidget(passwordEdit);

    // 登录按钮和注册按钮布局
    QHBoxLayout *loginButtonLayout = new QHBoxLayout;
    loginButton = new QPushButton("登录", this);
    registerButton = new QPushButton("注册", this);
    loginButtonLayout->addWidget(loginButton);
    loginButtonLayout->addWidget(registerButton);
    loginLayout->addLayout(loginButtonLayout);

    // 设置登录 QWidget
    QWidget *loginWidget = new QWidget(this);
    loginWidget->setLayout(loginLayout);
    setCentralWidget(loginWidget);

    // 连接信号与槽
    connect(loginButton, &QPushButton::clicked, this, &MainWindow::onLoginButtonClicked);
    connect(registerButton, &QPushButton::clicked, this, &MainWindow::onRegisterButtonClicked); // 连接槽
}

void MainWindow::showRegistrationForm() {
    QWidget *registrationWidget = new QWidget(this);
    QVBoxLayout *registrationLayout = new QVBoxLayout(registrationWidget);
    registrationLayout->setContentsMargins(40, 40, 40, 40);
    registrationLayout->setSpacing(15);

    QLabel *registrationTitleLabel = new QLabel("患者注册", this);
    registrationTitleLabel->setFont(QFont("Arial", 24, QFont::Bold));
    registrationTitleLabel->setAlignment(Qt::AlignCenter);
    registrationLayout->addWidget(registrationTitleLabel);

    // 控件: 患者ID
    idEdit = new QLineEdit(this);
    idEdit->setPlaceholderText("请输入您的患者ID");
    registrationLayout->addWidget(idEdit);

    // 控件: 姓名
    nameEdit = new QLineEdit(this);
    nameEdit->setPlaceholderText("请输入您的姓名");
    registrationLayout->addWidget(nameEdit);

    // 控件: 性别
    sexEdit = new QLineEdit(this);
    sexEdit->setPlaceholderText("请输入您的性别");
    registrationLayout->addWidget(sexEdit);

    // 控件: 年龄
    ageEdit = new QLineEdit(this);
    ageEdit->setPlaceholderText("请输入您的年龄");
    registrationLayout->addWidget(ageEdit);

    // 控件: 联系电话
    phoneEdit = new QLineEdit(this);
    phoneEdit->setPlaceholderText("请输入您的联系电话");
    registrationLayout->addWidget(phoneEdit);

    // 控件: 家庭住址
    addressEdit = new QLineEdit(this);
    addressEdit->setPlaceholderText("请输入您的家庭住址");
    registrationLayout->addWidget(addressEdit);

    // 控件: 登录密码
    passwardEdit = new QLineEdit(this);
    passwardEdit->setPlaceholderText("请输入您的登录密码");
    passwardEdit->setEchoMode(QLineEdit::Password);
    registrationLayout->addWidget(passwardEdit);

    // 注册和返回按钮布局
    QHBoxLayout *registrationButtonLayout = new QHBoxLayout;
    registerButtonSubmit = new QPushButton("注册", this);
    backButton = new QPushButton("返回", this);
    registrationButtonLayout->addWidget(registerButtonSubmit);
    registrationButtonLayout->addWidget(backButton);
    registrationLayout->addLayout(registrationButtonLayout);

    registrationWidget->setLayout(registrationLayout);
    setCentralWidget(registrationWidget);

    // 连接信号与槽
    connect(registerButtonSubmit, &QPushButton::clicked, this, &MainWindow::onRegisterSubmit);
    connect(backButton, &QPushButton::clicked, this, &MainWindow::setupUI);
}

void MainWindow::onRegisterButtonClicked() {
    showRegistrationForm(); // 显示注册表单
}

void MainWindow::onLoginButtonClicked() {
    if (roleComboBox->currentText() != "患者") {
        QMessageBox::warning(this, "错误", "此客户端仅限患者使用");
        return;
    }

    // 准备登录所需数据
    QMap<QString, QString> loginData;
    loginData.insert("type", PATIENTUSRDATABASE);
    loginData.insert("databaseoperator", QString::number(SELECT));
    loginData.insert("name", usernameEdit->text());
    loginData.insert("passward", passwordEdit->text());
    loginData.insert("senderType", PATIENTMESSAGE);
    QByteArray data = mapToByteArrayCustom(loginData); // 将数据转换为字节数组

    tcpSocket->connectToHost("127.0.0.1", 12345);

    if (tcpSocket->waitForConnected(3000)) {
        tcpSocket->write(data); // 向服务器发送数据
        tcpSocket->flush();
        if (tcpSocket->waitForReadyRead(3000)) {
            QByteArray responseData = tcpSocket->readAll();
            //qDebug() << "Server Response: " << responseData; // 调试信息，输出服务器响应内容
            if (!responseData.isEmpty()) {
                dashboard = new PatientDashboard(tcpSocket, this);
                dashboard->show();

                // 清空输入框
                usernameEdit->clear();
                passwordEdit->clear();

                this->hide(); // 隐藏登录窗口
            }
        } else {
            QMessageBox::warning(this, "登录失败", "服务器没有返回有效信息");
        }
    } else {
        QMessageBox::warning(this, "登录失败", "无法连接到服务器");
    }
}

void MainWindow::onRegisterSubmit() {
    QMap<QString, QString> registrationData;
    registrationData.insert("type", PATIENTUSRDATABASE);
    registrationData.insert("databaseoperator", QString::number(INSERT));
    registrationData.insert("id", idEdit->text());
    registrationData.insert("name", nameEdit->text());
    registrationData.insert("sex", sexEdit->text());
    registrationData.insert("age", ageEdit->text());
    registrationData.insert("phone", phoneEdit->text());
    registrationData.insert("address", addressEdit->text());
    registrationData.insert("passward", passwardEdit->text());

    QByteArray data = mapToByteArrayCustom(registrationData);
    tcpSocket->connectToHost("127.0.0.1", 12345);

    if (tcpSocket->waitForConnected(3000)) {
        tcpSocket->write(data);
        tcpSocket->flush();
        QMessageBox::information(this, "注册成功", "患者注册成功！");
        setupUI(); // 返回登录界面
    }
}

void MainWindow::onBackButtonClicked() {
    setupUI();
}
